package com.shop.cereshop.business.dl.logic.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.cereshop.business.dao.dl.CareShopNewsColumnDAO;
import com.shop.cereshop.business.dao.dl.CareShopNewsDAO;
import com.shop.cereshop.business.dl.logic.domian.CareShopNews;
import com.shop.cereshop.business.dl.logic.domian.CareShopNewsColumn;
import com.shop.cereshop.business.dl.logic.service.CareShopNewsService;
import com.shop.cereshop.business.dl.logic.utils.ListPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 况鑫威
 * @Date: 2021/6/15
 * @Time: 14:22
 * @ClassName: CareShopNewsServiceImpl
 * @Description: com.shop.cereshop.business.dl.logic.service.serviceImpl
 */
@Service
public class CareShopNewsServiceImpl implements CareShopNewsService {
    @Autowired
    private CareShopNewsDAO careShopNewsDao;
    @Autowired
    private CareShopNewsColumnDAO careShopNewsColumnDAO;

    @Override
    public int deleteByPrimaryKey(Long newsId) {
        return careShopNewsDao.deleteByPrimaryKey(newsId);
    }

    @Override
    public int insert(CareShopNews record) {
        return careShopNewsDao.insert(record);
    }

    @Override
    public int insertSelective(CareShopNews record, Long columnId) {
        if (record.getProductId()==0){
            record.setProductId(null);
        }
        int i = careShopNewsDao.insertSelective(record);
        CareShopNewsColumn careShopNewsColumn = new CareShopNewsColumn();
        careShopNewsColumn.setNewsId(record.getNewsId());
        careShopNewsColumn.setColumnid(columnId);
        careShopNewsColumnDAO.insertSelective(careShopNewsColumn);
        return i;
    }

    @Override
    public CareShopNews selectByPrimaryKey(Long newsId) {
        Long columnId = careShopNewsColumnDAO.selBynewsId(newsId);
        CareShopNews careShopNews = careShopNewsDao.selectByPrimaryKey(newsId);
        careShopNews.setColumnId(columnId);
        if (careShopNews.getProductId()==null){
            careShopNews.setProductId(0L);
        }
        return careShopNews;
    }

    @Override
    public int updateByPrimaryKeySelective(CareShopNews record, Long columnId) {
        if(columnId != null){
            careShopNewsColumnDAO.update(columnId, record.getNewsId());
        }
        return careShopNewsDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CareShopNews record) {
        return careShopNewsDao.updateByPrimaryKey(record);
    }

    @Override
    public int delNews(Long... newsIds) {
        int i1 = 0;
        for (int i = 0; i < newsIds.length; i++) {
            i1 += careShopNewsDao.deleteByPrimaryKey(newsIds[i]);
        }
        return i1;
    }

    @Override
    public List getNewsList(Long columnId, Integer page, Integer row,String title) {
        PageHelper.startPage(page, row);
        //查询所有对应栏目下的新闻
        List<CareShopNewsColumn> list = careShopNewsColumnDAO.getNewsList(columnId);
        ArrayList<CareShopNews> careShopNews = new ArrayList<>();
        //通过新闻id找出新闻具体
        for (CareShopNewsColumn careShopNewsColumn : list) {
            CareShopNews careShopNews1 = careShopNewsDao.selectByPrimaryKey2(careShopNewsColumn.getNewsId(),title);
            if(careShopNews1 != null){
            //转发数
            Integer a = careShopNewsDao.getForward(careShopNews1.getNewsId());
            //阅读数
            Integer b = careShopNewsDao.getView(careShopNews1.getNewsId());
            careShopNews1.setHits(b);
            careShopNews1.setRepeats(a);
                //添加到集合中
                careShopNews.add(careShopNews1);
            }
        }

//        List list1 = ListPageHelper.pageList(careShopNews, page, row);
        return careShopNews;
    }

    @Override
    public void updateByproductId(Long productId) {
        careShopNewsDao.updateByproductId(productId);
    }

    @Override
    public void updateBynewsId(Long newsId) {
        careShopNewsDao.updateBynewsId(newsId);
    }
}
